<?php
/**
 * @data   2017-11-15 15:22:57
 * @author huwhis@163.com
 * @version   0.0.1
 */

namespace app\admin\controller;

use app\admin\model\AdminUser as AdminUserModel;
use think\Request;

class AdminUser extends Base
{
    protected $adminuser_model;
    protected $adminuser_info;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new AdminUserModel();
        $this->adminuser_info = $this->model->field(true)->find($this->uid);
    }

    public function index()
    {
        $data = $this->model->where('is_del', 0)
            ->field('id,username,role_id,pen_name,email,create_time,status')
            ->select();

        $data_role = model('admin_role')->column('id, name');

        $this->assign("data", $data);
        $this->assign("data_role", $data_role);
        return $this->fetch();
    }
    
    public function addOrEdit($id = 0)
    {
        if ($id != 0) {
            $data = $this->model->find($id);
        } else {
            $data = ['id'=>0, 'username' => '', 'role_id' => 0, 'pen_name' => '', 'email' => '', 'note' => '',];
        }

        $data_role = model('admin_role')->where('status', 1)->column('id, name');

        $this->assign('data', $data);
        $this->assign("data_role", $data_role);
        return $this->fetch();
    }

    public function doAddorEdit(Request $request = null, $id = 0)
    {
        if ($request->isPost()) {
            $param = $request->param();
            if ($param['username'] =='' || $param['role_id'] =='') {
                $this->error("用户名 or 角色 不能为空!");
            }
            try {
                if ($param['id'] != 0) {
                    $this->model->save(
                        [
                        'username' => $param['username'],
                        'role_id' => intval($param['role_id']),
                        'pen_name' => $param['pen_name'],
                        'email' => $param['email'],
                        'note' => $param['note'],
                        ], ['id'=>$id]
                    );
                } else {
                    $password =  empty($param['password']) ? 123456 : $param['password'];
                    $this->model->save(
                        [
                        'username' => $param['username'],
                        'role_id' => intval($param['role_id']),
                        'password' => md5($password),
                        'pen_name' => $param['pen_name'],
                        'email' => $param['email'],
                        'note' => $param['note'],
                        'create_time' => time()
                        ]
                    );
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:".$msg);
            }
            $this->success('操作成功', 'index');
        }
    }

    public function isAvailable($id = null, $username = '')
    {
        if (Request::instance()->isAjax()) {
            $data = $this->model->where('username', $username)->find();
            if ($data && $data->id != $id) {
                return ['code' => 2, 'msg'=>'用户名已存在, 请使用其他用户名'];
            } else {
                return ['code' => 0, 'msg'=>'用户名可用'];
            }
        }
    }

    public function delete($id = null)
    {
        if (Request::instance()->isAjax()) {
            if (is_array($id)) {
                if (in_array(session('uid'), $id)) {
                    return ['code'=>0,'msg'=>'当前登录用户无法删除'];
                }
            } else {
                if ($id == session('uid')) {
                    return ['code'=>0,'msg'=>'当前登录用户无法删除'];
                }
            }
            if ($this->model->destroy($id)) {
                return ['code' => 1,'msg'=>'删除成功'];
            } else {
                return ['code' => 0,'msg'=>'删除失败'];
            }
        }
    }

    // 停用or启用管理员
    public function status($id, $status)
    {
        if (Request::instance()->isAjax()) {
            if ($id == session('uid')) {
                return ['code'=>0,'msg'=>'当前登录用户无法停用'];
            }
            if ($this->model->save(['status' => $status], ['id' => $id])) {
                return ['code'=>1,'msg'=>'操作成功'];
            } else {
                return ['code'=>0,'msg'=>'操作失败'];
            }
        }
    }

    //修改密码
    public function modifyPwd()
    {
        if (Request::instance()->isPOST()) {
            $pwd = $this->adminuser_info->password;
            $oldpwd = md5(trim(input('post.oldpassword')));
            $newpwd = md5(trim(input('post.newpassword')));
            $repwd = md5(trim(input('post.repassword')));
            if ($pwd!=$oldpwd) {
                $this->error('原密码不正确');
            }
            if ($newpwd!=$repwd) {
                $this->error('两次新密码不一致，请核查');
            }
            $this->adminuser_info->password = $newpwd;
            if ($this->adminuser_info->save()) {
                session(null);
                $this->success("修改成功,请重新登陆", 'login/logout');
            } else {
                $this->error('修改失败，请稍后重试');
            }
        } else {
            return $this->fetch();
        }
    }
}
